package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Mr.M
 * @version 1.0
 * @description TODO
 * @date 2024/10/24 9:15
 */
@Component
@Slf4j
public class SpringRabbitListener {

//    //监听队列simple.queue 消息
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg) {
//        log.info("spring-boot-rabbitmq 接收到simple.queue的消息：{}", msg);
//    }

//    @RabbitListener(queues = "work.queue")
//    public void listenWorkQueue1(String msg) throws InterruptedException {
//        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
//        Thread.sleep(20);
//    }
//
//    @RabbitListener(queues = "work.queue")
//    public void listenWorkQueue2(String msg) throws InterruptedException {
//        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
//        Thread.sleep(200);
//    }

    /**
     * 广播模式-fanout
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue2(String msg) {
        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue3(String msg) {
        System.out.println("消费者3接收到Fanout消息：【" + msg + "】");
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue4(String msg) {
        System.out.println("消费者4接收到Fanout消息：【" + msg + "】");
    }

    /**
     * 路由模式-direct
     * @param msg
     */
    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1接收到direct.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者2接收到direct.queue2的消息：【" + msg + "】");
    }

    /**
     * 主题模式-topic
     * @param msg
     */
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者1接收到top.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg) {
        System.out.println("消费者2接收到top.queue2的消息：【" + msg + "】");
    }

    /**
     * 注解方式创建交换机和队列以及绑定关系
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "direct.queue1", durable = "true"),
            exchange = @Exchange(value = "hmall.direct", type = ExchangeTypes.DIRECT, durable = "true"),//指定交换机
            key = {"red", "blue"}
    ))
    public void listenDirectQueue01(String msg) {
        System.out.println("消费者01接收到direct.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "direct.queue2", durable = "true"),
            exchange = @Exchange(value = "hmall.direct", type = ExchangeTypes.DIRECT, durable = "true"),//指定交换机
            key = {"red", "yellow"}
    ))
    public void listenDirectQueue02(String msg) {
        System.out.println("消费者02接收到direct.queue02的消息：【" + msg + "】");
    }

    /**
     * 接受Object
     * @param msg
     * @throws InterruptedException
     */
    @RabbitListener(queues = "object.queue")
    public void listenSimpleQueueMessage(Map<String, Object> msg) throws InterruptedException {
        System.out.println("消费者接收到object.queue消息：【" + msg + "】");
    }

    /**
     * 惰性队列-lazy
     * @param msg
     */

    @RabbitListener(queuesToDeclare = @Queue(
            name = "lazy.queue",
            durable = "true",
            arguments = @Argument(name = "x-queue-mode",value = "lazy")
    ))
    public void listenLazyQueue(String msg){
        log.info("接收到 lazy.queue的消息：{}", msg);
    }

    /**
     * 优先级队列
     * @param msg
     * @throws InterruptedException
     */
    //监听优先级队列
    @RabbitListener(queuesToDeclare = @Queue(
            name="priority.queue",
            durable = "true",
            arguments = @Argument(name = "x-max-priority", value = "10",type="java.lang.Integer")))
    public void listenPriorityQueue(String msg) throws InterruptedException {
        System.out.println("消费者接收到priority.queue消息：【" + msg + "】");
    }

}
